﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_Views_common_login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginWarning.Visible = false;
        GetErrorString();
        if (Request.Cookies["RememberMe"] != null)
        {
            HttpCookie cookieRememberMe = Request.Cookies.Get("RememberMe");
            txtUsername.Text = cookieRememberMe.Values["UserName"].ToString();
            txtPassword.Attributes.Add("Value", cookieRememberMe.Values["Password"].ToString());

            if (cookieRememberMe.Values["CheckBox"].ToString() == "True")
            {
                chkRemember.Checked = true;
            }
            else
            {
                chkRemember.Checked = false;
            }
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        AccountBus accBus = new AccountBus();
        string username = txtUsername.Text;
        string pass = txtPassword.Text;
        if (accBus.Checklogin(username,pass))
        {
            Session.Clear();
            HttpCookie cookieRememberMe = new HttpCookie("RememberMe");
            Session["username"] = txtUsername.Text;
            if (chkRemember.Checked == true)
            {
                if (Request.Cookies["RememberMe"] != null)
                {
                    cookieRememberMe = new HttpCookie("RememberMe");
                    cookieRememberMe.Values.Add("UserName", "");
                    cookieRememberMe.Values.Add("Password", "");
                    cookieRememberMe.Values.Add("CheckBox", "");
                    cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(cookieRememberMe);
                }

                cookieRememberMe = new HttpCookie("RememberMe");
                cookieRememberMe.Values.Add("UserName", txtUsername.Text.Trim());
                cookieRememberMe.Values.Add("Password", txtPassword.Text.Trim());
                cookieRememberMe.Values.Add("CheckBox", "True");
                cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(cookieRememberMe);
            }
            else
            {
                if (Request.Cookies["RememberMe"] != null)
                {
                    cookieRememberMe = new HttpCookie("RememberMe");
                    cookieRememberMe.Values.Add("UserName", txtUsername.Text.Trim());
                    cookieRememberMe.Values.Add("Password", "");
                    cookieRememberMe.Values.Add("CheckBox", "");
                    cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                    Response.Cookies.Add(cookieRememberMe);
                }
            }
        }else{
            Session["username"] = "";
            Response.Redirect("default.aspx?error=100");
        }
        
        Response.Redirect("default.aspx");
    }

    public void GetErrorString()
    {
        string errorQuery = Request.QueryString["error"];
        string errorString = "";

        if (errorQuery != null || errorQuery != "")
        {
            if (errorQuery == "100")
            {
                errorString = "Sai tên đăng nhập hoặc mật khẩu.";
            }

            if (errorString != "")
            {
                LoginWarning.Visible = true;
                lblLoginError.Text = errorString;
            }            
        }
    }
}